package com.oa.modules.approvalsec1.controller;

import com.oa.common.utils.R;
import com.oa.modules.approvalsec1.service.ChatService;
import com.oa.modules.approvalsec1.socket.SocketServer;
import com.oa.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * @Author: Hankaicheng
 * @Date: 2022/3/1 15:59
 */
@Api("小程序聊天相关接口")
@RestController
@RequestMapping("chat")
@CrossOrigin
public class ChatController extends AbstractController {

    @Value("${url.saveUrl}")
    private String url;

    @Autowired
    private ChatService chatService;

    @Autowired
    private SocketServer socketServer;


    @ApiOperation(value = "获取常见问题")
    @PostMapping("/questions")
    public Map<String, Object> questions(@RequestBody Map<String, Object> params) {
        Map<String, Object> questions = chatService.getQuestions(params);
        return questions;
    }

    /**
     * 文件上传
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadFile")
    public R uploadFile(HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
        request.setCharacterEncoding("utf-8");
        MultipartFile mfile = req.getFile("file");
        File file = new File(url + "image/examination-approval-project/" + mfile.getOriginalFilename());
        FileUtils.copyInputStreamToFile(mfile.getInputStream(), file);
        if (file != null && file.exists()) {
            Map<String, Object> result = chatService.uploadFile(file);
            file.delete();
            if (result.get("data") != null) {
                Map data = (Map) result.get("data");
                return R.ok().put("data", data);
            } else {
                return R.error();
            }
        }
        return R.ok();
    }

    /**
     * 发送消息
     *
     * @param params
     * @return
     */
    @PostMapping("/sendMessage")
    public R sendMessage(@RequestBody Map<String, Object> params) {
        Map<String, Object> result = chatService.sendMessage(params);
        return R.ok().put("data", result);
    }

    /**
     * 保存评价
     * @param params
     * @return
     */
    @PostMapping("/evaluation")
    public Map<String, Object> evaluation(@RequestBody Map<String, Object> params){
        Map<String, Object> result = chatService.evaluation(params);
        return result;
    }


}
